/* OutListBusiness.java
 *
 * Project/Product: GW
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   Administrator	2008-7-9	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2008
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package system.download;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.Vector;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import framework.core.DBConnection;
import framework.core.DBEngine;
import framework.core.DBResult;

public class OutListBusiness {
    private Business business = null;

    OutListBusiness() {
        business = Business.getBusiness();
    }

    String[] colNamesOut = new String[] { "OUT_BILL_CODE", "CUSTOMER_BUS_CODE", "WAREHOUSE_CODE", "WAREHOUSE_NAME", "OUT_MONEY", "CREATE_TIME", "CREATE_USER",
            "GET_CUSTOMER_STAFF_CODE", "GET_CUSTOMER_STAFF_NAME", "GET_CUSTOMER_NAME", "PRODUCT_CODE", "ID", "PRODUCT_NAME", "PRODUCT_COUNT", "PRODUCT_PRICE",
            "PRODUCT_MONEY", };

    public HSSFWorkbook createOuthead(String preDay, String sheetName, Vector vec) {
        //创建文件对象
        HSSFWorkbook tWb = new HSSFWorkbook();
        //创建sheet
        HSSFSheet tSheet = tWb.createSheet();
        //命名sheet
        tWb.setSheetName(0, sheetName, HSSFCell.ENCODING_UTF_16);
        //创建行
        HSSFRow tRow = tSheet.createRow((short) 0);
        int size = vec.size();
        double money = 0;
        for (int i = 0; i < size; i++) {
            Map map = (Map) vec.get(i);
            double cash = Double.parseDouble(String.valueOf(map.get("OUT_MONEY")));
            String time = String.valueOf(map.get("CREATE_TIME"));
            time = business.get14Time(time);
            money += cash;
            HSSFRow row = tSheet.createRow((short) i + 1);
            //领料单号
            HSSFCell cell0 = business.createCell(tWb, row, (short) 0, map.get("OUT_BILL_CODE") == null ? "" : String.valueOf(map.get("OUT_BILL_CODE")));
            //车号
            HSSFCell cell1 = business.createCell(tWb, row, (short) 1, map.get("CUSTOMER_BUS_CODE") == null ? "" : String.valueOf(map.get("CUSTOMER_BUS_CODE")));
            //发料单位
            HSSFCell cell2 = business.createCell(tWb, row, (short) 2, map.get("WAREHOUSE_NAME") == null ? "" : String.valueOf(map.get("WAREHOUSE_NAME")));
            //总价
            HSSFCell cell3 = business.createCell(tWb, row, (short) 3, String.valueOf(cash * 100));
            //领料时间
            HSSFCell cell4 = business.createCell(tWb, row, (short) 4, time);
            //申请时间
            HSSFCell cell5 = business.createCell(tWb, row, (short) 5, "");
            //发料人职号
            HSSFCell cell6 = business.createCell(tWb, row, (short) 6, "");
            //发料人姓名
            HSSFCell cell7 = business.createCell(tWb, row, (short) 7, map.get("CREATE_USER") == null ? "" : String.valueOf(map.get("CREATE_USER")));
            //领料人职号
            HSSFCell cell8 = business.createCell(tWb, row, (short) 8, map.get("GET_CUSTOMER_STAFF_CODE") == null ? "" : String.valueOf(map
                    .get("GET_CUSTOMER_STAFF_CODE")));
            //领料人姓名
            HSSFCell cell9 = business.createCell(tWb, row, (short) 9, map.get("GET_CUSTOMER_STAFF_NAME") == null ? "" : String.valueOf(map
                    .get("GET_CUSTOMER_STAFF_NAME")));
            //领料单位
            HSSFCell cell10 = business.createCell(tWb, row, (short) 10, map.get("GET_CUSTOMER_NAME") == null ? "" : String
                    .valueOf(map.get("GET_CUSTOMER_NAME")));
            //保修单号
            HSSFCell cell11 = business.createCell(tWb, row, (short) 11, "");
        }
        HSSFCell tCell0 = business.createCell(tWb, tRow, (short) 0, "总金额数:");
        HSSFCell tCell1 = business.createCell(tWb, tRow, (short) 1, String.valueOf(money * 100));
        return tWb;
    }

    /** 创建领料单明细 */
    public HSSFWorkbook createOutsub(String preDay, String sheetName, Vector vec) {
        //创建文件对象
        HSSFWorkbook tWb = new HSSFWorkbook();
        //创建sheet
        HSSFSheet tSheet = tWb.createSheet();
        //命名sheet
        tWb.setSheetName(0, sheetName, HSSFCell.ENCODING_UTF_16);
        //创建行
        HSSFRow tRow = tSheet.createRow((short) 0);
        int size = vec.size();
        double money = 0;
        for (int i = 0; i < size; i++) {
            Map map = (Map) vec.get(i);
            double cash = Double.parseDouble(String.valueOf(map.get("PRODUCT_MONEY")));
            double price = Double.parseDouble(String.valueOf(map.get("PRODUCT_PRICE")));
            money += cash;
            HSSFRow row = tSheet.createRow((short) i + 1);
            HSSFCell cell0 = business.createCell(tWb, row, (short) 0, map.get("OUT_BILL_CODE") == null ? "" : String
                    .valueOf(map.get("OUT_BILL_CODE")));
            HSSFCell cell1 = business.createCell(tWb, row, (short) 1, map.get("PRODUCT_CODE") == null ? "" : String.valueOf(map.get("PRODUCT_CODE")));
            HSSFCell cell2 = business.createCell(tWb, row, (short) 2, map.get("ID") == null ? "" : String.valueOf(map.get("ID")));
            HSSFCell cell3 = business.createCell(tWb, row, (short) 3, map.get("PRODUCT_NAME") == null ? "" : String.valueOf(map.get("PRODUCT_NAME")));
            HSSFCell cell4 = business.createCell(tWb, row, (short) 4, map.get("PRODUCT_COUNT") == null ? "" : String.valueOf(map.get("PRODUCT_COUNT")));
            HSSFCell cell5 = business.createCell(tWb, row, (short) 5, String.valueOf(price * 100));
            HSSFCell cell6 = business.createCell(tWb, row, (short) 6, String.valueOf(cash * 100));
        }
        HSSFCell tCell0 = business.createCell(tWb, tRow, (short) 0, "总金额数:");
        HSSFCell tCell1 = business.createCell(tWb, tRow, (short) 1, String.valueOf(money * 100));
        return tWb;
    }

    /**   提取单一的入库单*/
    private Vector createHeadVectorByBill(Vector vec) {
        Vector newVec = new Vector();
        int size = vec.size();
        for (int i = 0; i < size; i++) {
            Map map1 = null;
            Map map2 = null;
            if (i == 0) {
                map1 = (Map) vec.get(i);
                newVec.add(map1);
            }
            if (size == 1) {
                return newVec;
            }
            if(i == size - 1 ){
                return newVec;
            }
            map1 = (Map) vec.get(i);
            map2 = (Map) vec.get(i + 1);
            String code1 = String.valueOf(map1.get("OUT_BILL_CODE"));
            String code2 = String.valueOf(map2.get("OUT_BILL_CODE"));
            if (!code1.equals(code2)) {
                newVec.add(map2);
            }
            if (i == size - 1) {
                return newVec;
            }
        }
        return newVec;
    }


    public void searchOuthead(String preDay, DBConnection con) {
        Map map = null;
        //获得路径
        String path = FileBusiness.getRealPath();
        //返回所有的仓库码
        Vector wareVec = searchAllWarehouseCode(con);
        int wareSize = wareVec.size();
        //分仓库生成出库子单文件
        for (int i = 0; i < wareSize; i++) {
            String warehouseCode = String.valueOf(wareVec.get(i));
            String warehousePath = path + warehouseCode + "/";
            //根据仓库查询出库
            Vector singleWareOut = searchOutListByWarehouse(preDay, con, warehouseCode);
            int singleWareOutSize = singleWareOut.size();
            if (singleWareOutSize > 0) {
                String fileName1 = business.getPreDayNum()+"_"+warehouseCode+"_"+"出库主单.xls";
                String fileName2 = business.getPreDayNum()+"_"+warehouseCode+"_"+"出库子单.xls";
                Vector headVec = createHeadVectorByBill(singleWareOut);
                //int headVecSize = headVec.size();
                HSSFWorkbook book1 = createOuthead(preDay, "出库主单", headVec);
                HSSFWorkbook book2 = createOutsub(preDay, "出库子单", singleWareOut);
                boolean document = true;
                if (!FileBusiness.isFileExists(warehousePath)) {
                    document = FileBusiness.createDocument(warehousePath);
                }
                if(!document){
                    map = business.createMap(warehouseCode, "T_OUT_HEAD", path, fileName1, "失败");
                    business.writeReport(con, map);
                    map = business.createMap(warehouseCode, "T_OUT_SUB", path, fileName2, "失败");
                    business.writeReport(con, map);
                    continue;
                }
                //写文件
                boolean result1 = FileBusiness.createExcelFile(warehousePath, book1, fileName1);
                //写文件
                boolean result2 = FileBusiness.createExcelFile(warehousePath, book2, fileName2);
                //写报告
                if (result1){
                    map = business.createMap(warehouseCode, "T_OUT_HEAD", path, fileName1, "成功");
                    business.writeReport(con, map);
                }else{
                    map = business.createMap(warehouseCode, "T_OUT_HEAD", path, fileName1, "失败");
                    business.writeReport(con, map);
                }
                //写报告
                if (result2){
                    map = business.createMap(warehouseCode, "T_OUT_SUB", path, fileName2, "成功");
                    business.writeReport(con, map);
                }
                else{
                    map = business.createMap(warehouseCode, "T_OUT_SUB", path, fileName2, "失败");
                    business.writeReport(con, map);
                }
            } else {
                map = business.createMap(warehouseCode, "T_OUT_SUB", path, "", "无更新");
                business.writeReport(con, map);
                map = business.createMap(warehouseCode, "T_OUT_HEAD", path, "", "无更新");
                business.writeReport(con, map);
            }
        }
    }

    public void searchOutsub(Vector vec, String preDay) {
        if (vec.size() > 0) {
            //创建文件
            //写报告
        } else {
            //写报告
        }
        //若有文件   写入磁盘
    }

    /**   查询所有的仓库列表*/
    private Vector searchAllWarehouseCode(DBConnection con) {
        DBResult dbrs = null;
        ResultSet rs = null;
        Vector vec = new Vector();
        String sql = "select code from T_WAREHOUSE";
        try {
            dbrs = DBEngine.doSelect(con, sql);
            rs = dbrs.getRs();
            while (rs.next()) {
                vec.add(rs.getString("CODE"));
            }
        } catch (SQLException e) {
            
            e.printStackTrace();
        } catch (Exception e) {
            
            e.printStackTrace();
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (dbrs != null)
                    dbrs.clear();
            } catch (SQLException e) {
                
                e.printStackTrace();
            } catch (Exception e) {
                
                e.printStackTrace();
            }
        }
        return vec;
    }

    /**   处理出库 以仓库为单位 生成文件*/
    private void set(Vector vec, DBConnection con) {
        //获得所有仓库
        Vector wareList = this.searchAllWarehouseCode(con);
        int wsize = wareList.size();
        //遍历仓库
        int size = vec.size();
        Vector vecAll = new Vector();
        for (int i = 0; i < wsize; i++) {
            Vector wareVec = new Vector();
            //遍历所有新出库
            String code = String.valueOf(wareList.get(i));
            for (int j = 0; j < size; j++) {
                Map temp = (Map) vec.get(j);
                String code1 = String.valueOf(temp.get("WAREHOUSE_CODE"));
                if (code.equals(code1)) {
                    //code相同时 加入到集合中 形成一个 由 相同warhousecode构成的出库信息
                    wareVec.add(temp);
                }
            }
            vecAll.add(wareVec);
        }
        int vecallsize = vecAll.size();
        //开始写文件
        for (int i = 0; i < vecallsize; i++) {

        }
    }

    /**   根据仓库查出库信息*/
    Vector searchOutListByWarehouse(String preDay, DBConnection con, String warehouseCode) {
        String sql = 
            "select "+ 
                "head.OUT_BILL_CODE,"+
                "head.CUSTOMER_BUS_CODE,"+
                "head.WAREHOUSE_CODE,"+
                "head.WAREHOUSE_NAME,"+
                "head.OUT_MONEY,"+
                "head.CREATE_TIME,"+
                "head.CREATE_USER,"+
                "sub.GET_CUSTOMER_STAFF_CODE,"+
                "sub.GET_CUSTOMER_STAFF_NAME,"+
                "sub.GET_CUSTOMER_NAME,"+
                "sub.PRODUCT_CODE,"+
                "sub.ID,"+
                "sub.PRODUCT_NAME,"+
                "sub.PRODUCT_COUNT,"+
                "sub.PRODUCT_PRICE,"+
                "sub.PRODUCT_MONEY "+
            "from "+
                "t_out_head head,"+
                "t_out_sub sub "+
            "where "+
                    "head.last_update_time >= '"+preDay+business.start+"' "+ 
                "and head.last_update_time <= '"+preDay+business.end+"' "+
                "and sub.last_update_time >= '"+preDay+business.start+"' "+
                "and sub.last_update_time <= '"+preDay+business.end+"' "+
                "and head.out_bill_code = sub.out_bill_code "+
                "and head.WAREHOUSE_CODE = '"+warehouseCode+"'"+
            "order by head.out_bill_code";
        //同一仓库的所有出库信息
        Vector vec = business.setMap(colNamesOut, sql, con);
        return vec;
    }
}
